* This do-file reproduces the results for Table 2 from article
* Also reproduces results found in Tables A2 and A5 of Supplementary Information

* Open the wide-form version of the 2010-2014 CCES Panel Survey
use cces_panel_2010_2014.dta, clear

***** This do-file first recodes the variables and then re-saves the dataset so that
***** it can be stacked in long-form for use in panel time series estimation

****** 2014 Wave Re-coding

* Generate variables for whether R thinks MC matches/mismatches population of district on race/ethnicity
gen racematch=1 if (CC14_306a==2 &  CC14_388a==3) | (CC14_306a==3 &  CC14_388a==1) | (CC14_306e==2 &  CC14_388a==3 & HouseCand1IncumbentNum==1) | (CC14_306f==2 &  CC14_388a==3 & HouseCand2IncumbentNum==1)
gen racemismatch=1 if (CC14_306a==2 &  CC14_388a~=3 ) | (CC14_306a==3 &  CC14_388a~=1 ) | (CC14_306e==2 &  CC14_388a~=3 & HouseCand1IncumbentNum==1) | (CC14_306f==2 &  CC14_388a~=3 & HouseCand2IncumbentNum==1)
recode racematch-racemismatch (.=0)

* Generate variables for whether R thinks MC matches/mismatches population of district on partisanship
gen partydistmatch=1 if CurrentHouseParty=="Republican" & CC14_388b==2
replace partydistmatch=1 if CurrentHouseParty=="Democratic" & CC14_388b==1
recode partydistmatch .=0

gen partydistmismatch=1 if CurrentHouseParty=="Republican" & CC14_388b==1
replace partydistmismatch=1 if CurrentHouseParty=="Democratic" & CC14_388b==2
recode partydistmismatch .=0

* Generate variable for R's self reported ideology
recode CC14_341A 8=., gen(idself)

* Generate variable for R's placement of Incumbent on ideological scale
gen idmember=CC14_341K if HouseCand1IncumbentNum==1
replace idmember=CC14_341L if HouseCand2IncumbentNum==1
replace idmember=CC14_341M if CC14_341M~=.
recode idmember 8=.

* Generate variable for R's Party ID
recode pid7_14 1/3=1 4=0 5/7=-1 8=0, gen(partyid)

* Genenerate variable for Party of Incumbent
gen inc=-1 if CurrentHouseParty=="Republican"
replace inc=1 if CurrentHouseParty=="Democratic"
replace inc=0 if inc==.

* Generate variable for whether R and MC share party
gen sameparty=2 if (partyid==1 & inc==1) | (partyid==-1 & inc==-1)
replace sameparty=0 if (partyid==1 & inc==-1) | (partyid==-1 & inc==1)
replace sameparty=1 if partyid==0 & inc~=0

* Generate variable for R's distance from MC
gen distance=abs(idself-idmember)

* Generate variable for number of issues on which R and MC agree
gen agreement=0
replace agreement=agreement+1 if CC14_322G==CC14_333_a_1 & CC14_322G~=. & CC14_333_a_1~=. 
replace agreement=agreement+1 if CC14_333_a_3==CC14_322H & CC14_333_a_3~=. & CC14_322H~=.
replace agreement=agreement+1 if CC14_333_a_4==CC14_332E & CC14_333_a_4~=. & CC14_332E~=.
replace agreement=agreement+1 if CC14_333_a_5==CC14_332B & CC14_333_a_5~=. & CC14_332B~=.
replace agreement=agreement+1 if CC14_333_a_2==CC14_332A & CC14_333_a_2~=. & CC14_332A~=.

* Rename and recode variables for how well MC represents district and MC approval
rename CC14_367 repdistrict

* Generate dummies for the categories of repdist
tab repdistrict, gen(repdist)
ren repdist1 verywell
ren repdist3 notwell
ren repdist2 somewhatwell

* Generate version of DV that excludes not sure and reorders categories
recode repdistrict 1=3 3=1 4=., gen(repdistrict2)

* Generate approval
recode CC14_315a 1=4 2=3 3=2 4=1 5=., gen(approval)

** The next commands are to use the current house name text to determine if 
** R's house member changed from 2012 to 2014

gen rev_name_2012=reverse(CurrentHouseName_wave2012)
gen rev_name_2014=reverse(CurrentHouseName)

gen lastnameend2012=index(rev_name_2012, " ")
gen lastnameend2014=index(rev_name_2014, " ")

gen temp=indexnot(rev_name_2012, rev_name_2014)
gen mcchange=1 if temp<lastnameend2012 & temp~=0

* Manual commands for names that don't match
replace mcchange=. if index(CurrentHouseName, "Sensenbrenner")>0 & index(CurrentHouseName_wave2012, "Sensenbrenner")>0
replace mcchange=. if index(CurrentHouseName, "Carney")>0 & index(CurrentHouseName_wave2012, "Carney")>0
replace mcchange=. if index(CurrentHouseName, "Moran")>0 & index(CurrentHouseName_wave2012, "Moran")>0
replace mcchange=. if index(CurrentHouseName, "Johnson")>0 & index(CurrentHouseName_wave2012, "Johnson")>0 & index(CurrentHouseName, "Hank")>0 & index(CurrentHouseName_wave2012, "Hank")>0
replace mcchange=. if index(CurrentHouseName, "Cleaver")>0 & index(CurrentHouseName_wave2012, "Cleaver")>0 & index(CurrentHouseName, "Emanuel")>0 & index(CurrentHouseName_wave2012, "Emanuel")>0
replace mcchange=. if index(CurrentHouseName, "Rahall")>0 & index(CurrentHouseName_wave2012, "Rahall")>0 & index(CurrentHouseName, "Nick")>0 & index(CurrentHouseName_wave2012, "Nick")>0
replace mcchange=. if index(CurrentHouseName, "Reed")>0 & index(CurrentHouseName_wave2012, "Reed")>0 & index(CurrentHouseName, "Tom")>0 & index(CurrentHouseName_wave2012, "Thomas")>0

gen nopartychange=1 if CurrentHouseParty==CurrentHouseParty_wave2012

* Prepare dataset for saving, keeping only variables we will need for our analysis
keep V101 weight racematch- nopartychange repdistrict
* Generate a variable for year
gen year=2014
* Save version of dataset which amounts to the 2014 wave of the panel
save wave2014, replace


******* 2012 Wave Re-coding

* Re-open original dataset and re-code variables again, but this time for 2012 values
use cces_panel_2010_2014.dta, clear

* Generate variables for whether R thinks MC matches/mismatches population of district on race/ethnicity
gen racematch=1 if (CC12_306a==2 &  CC12_388a==3) | (CC12_306a==3 &  CC12_388a==1) | (CC12_306e==2 &  CC12_388a==3 & HouseCand1IncumbentNum==1) | (CC12_306f==2 &  CC12_388a==3 & HouseCand2IncumbentNum==1)
gen racemismatch=1 if (CC12_306a==2 &  CC12_388a~=3 ) | (CC12_306a==3 &  CC12_388a~=1 ) | (CC12_306e==2 &  CC12_388a~=3 & HouseCand1IncumbentNum==1) | (CC12_306f==2 &  CC12_388a~=3 & HouseCand2IncumbentNum==1)
recode racematch-racemismatch (.=0)

* Generate variables for whether R thinks MC matches/mismatches population of district on partisanship
gen partydistmatch=1 if CurrentHouseParty_wave2012=="Republican" & CC12_388b==2
replace partydistmatch=1 if CurrentHouseParty_wave2012=="Democratic" & CC12_388b==1
recode partydistmatch .=0

gen partydistmismatch=1 if CurrentHouseParty_wave2012=="Republican" & CC12_388b==1
replace partydistmismatch=1 if CurrentHouseParty_wave2012=="Democratic" & CC12_388b==2
recode partydistmismatch .=0

* Generate variable for R's self reported ideology
recode CC12_341A 8=., gen(idself)

* Generate variable for R's placement of Incumbent on ideological scale
gen idmember=CC12_341K if HouseCand1Incumbent_wave2012=="1"
replace idmember=CC12_341L if HouseCand2Incumbent_wave2012=="1"
replace idmember=CC12_341M if CC12_341M~=.
recode idmember 8=.

* Generate variable for R's Party ID
recode pid7_wave2012 1/3=1 4=0 5/7=-1 8=0, gen(partyid)

* Genenerate variable for Party of Incumbent
gen inc=-1 if CurrentHouseParty_wave2012=="Republican"
replace inc=1 if CurrentHouseParty_wave2012=="Democratic"
replace inc=0 if inc==.

* Generate variable for whether R and MC share party
gen sameparty=2 if (partyid==1 & inc==1) | (partyid==-1 & inc==-1)
replace sameparty=0 if (partyid==1 & inc==-1) | (partyid==-1 & inc==1)
replace sameparty=1 if partyid==0 & inc~=0

* Generate variable for R's distance from MC
gen distance=abs(idself-idmember)

* Generate variable for number of issues on which R and MC agree
gen agreement=0
replace agreement=agreement+1 if CC322G_wave2012==CC12_333_a_1 & CC322G_wave2012~=. & CC12_333_a_1~=.
replace agreement=agreement+1 if CC12_333_a_3==CC322H_wave2012 & CC322H_wave2012~=. & CC12_333_a_3~=.
replace agreement=agreement+1 if CC12_333_a_4==CC332E_wave2012 & CC332E_wave2012~=. & CC12_333_a_4~=.
replace agreement=agreement+1 if CC12_333_a_5==CC332B_wave2012 & CC332B_wave2012~=. & CC12_333_a_5~=.
replace agreement=agreement+1 if CC12_333_a_2==CC332A_wave2012 & CC332A_wave2012~=. & CC12_333_a_2~=.

* Rename and recode variables for how well MC represents district and MC approval
rename CC12_367 repdistrict

* Generate dummies for the categories of repdist
tab repdistrict, gen(repdist)
ren repdist1 verywell
ren repdist3 notwell
ren repdist2 somewhatwell

* Generate version of DV that excludes not sure and reorders categories
recode repdistrict 1=3 3=1 4=., gen(repdistrict2)

* Generate approval
recode CC12_315a 1=4 2=3 3=2 4=1 5=., gen(approval)


** The next commands are to use the current house name text to determine if 
** R's house member changed from 2012 to 2014

gen rev_name_2012=reverse(CurrentHouseName_wave2012)
gen rev_name_2014=reverse(CurrentHouseName)

gen lastnameend2012=index(rev_name_2012, " ")
gen lastnameend2014=index(rev_name_2014, " ")

gen temp=indexnot(rev_name_2012, rev_name_2014)
gen mcchange=1 if temp<lastnameend2012 & temp~=0

* Manual commands for names that don't match
replace mcchange=. if index(CurrentHouseName, "Sensenbrenner")>0 & index(CurrentHouseName_wave2012, "Sensenbrenner")>0
replace mcchange=. if index(CurrentHouseName, "Carney")>0 & index(CurrentHouseName_wave2012, "Carney")>0
replace mcchange=. if index(CurrentHouseName, "Moran")>0 & index(CurrentHouseName_wave2012, "Moran")>0
replace mcchange=. if index(CurrentHouseName, "Johnson")>0 & index(CurrentHouseName_wave2012, "Johnson")>0 & index(CurrentHouseName, "Hank")>0 & index(CurrentHouseName_wave2012, "Hank")>0
replace mcchange=. if index(CurrentHouseName, "Cleaver")>0 & index(CurrentHouseName_wave2012, "Cleaver")>0 & index(CurrentHouseName, "Emanuel")>0 & index(CurrentHouseName_wave2012, "Emanuel")>0
replace mcchange=. if index(CurrentHouseName, "Rahall")>0 & index(CurrentHouseName_wave2012, "Rahall")>0 & index(CurrentHouseName, "Nick")>0 & index(CurrentHouseName_wave2012, "Nick")>0
replace mcchange=. if index(CurrentHouseName, "Reed")>0 & index(CurrentHouseName_wave2012, "Reed")>0 & index(CurrentHouseName, "Tom")>0 & index(CurrentHouseName_wave2012, "Thomas")>0

gen nopartychange=1 if CurrentHouseParty==CurrentHouseParty_wave2012

* Keep the 2012 version of the variables only
keep V101 weight racematch- nopartychange repdistrict
* Generate indicator for the appropriate year
gen year=2012

* Stack datasets by appending 2014 (note all variable names are the same in both datasets)
append using wave2014

* Set the dataset for panel time series analysis. V101 indicates Respondent ID and year indicates time
xtset V101 year


**** RESULTS

* Produce results for models in Table 2
xtreg approval distance sameparty [aw=weight] if mcchange==1 & repdistrict2<4, fe
xtreg approval repdistrict2 distance sameparty [aw=weight] if mcchange==1 & repdistrict2<4, fe

* Produce results for Table A2 in Supplementary Information
xtreg approval agreement sameparty [aw=weight] if mcchange==1 & repdistrict2<4, fe
xtreg approval repdistrict2 agreement sameparty [aw=weight] if mcchange==1 & repdistrict2<4, fe

* Produce results for model 4 in Table A5 in Supplementary Information
xtreg approval i.repdistrict distance sameparty [aw=weight] if mcchange==1 , fe


